<?php

class SearchController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    /*
     * Отображение секции
     */

    public function indexAction() {
        $filter = new Zend_Filter_Alnum(true);
        $query = $filter->filter($this->_request->getPost('search', $this->_getParam('query', '')));
        
        if ($this->view->config->etrace == 1)
                $query = '';
        
        if (mb_strlen($query) < 3) {
            if ($query == '') {
                $this->view->error = '0';
            } else {
                $this->view->error = '1';
            }
        } else {
            Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
            Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
            $model = new SearchModel();
            $query = Zend_Search_Lucene_Search_QueryParser::parse($query . '^4', 'UTF-8');
            $index = Zend_Search_Lucene::open(APP_PATH . '/index');
            $hits = $index->find($query);
            if (count($hits) == 0) {
                $this->view->error = '1';
            } else {
                foreach ($hits as $hit) {
                    //print_r($hit); exit;
                    $words = explode(" ", $queryStr);
                    $string = $hit->body;
                    $string = $model->excerpt($string, $queryStr, 500);
                    //$string = $query->highlightMatches($string);
                    foreach ($words as $word)
                        $string = $this->mb_str_ireplace($word, '<span class="searchlight">' . $word . '</span>', $string);

                    $results[$hit->page_id]['content'] = $string;
                    $results[$hit->page_id]['page_url'] = $hit->page_url;
                    $results[$hit->page_id]['page_title'] = $hit->page_title;
                }

                $this->view->results = $results;
            }
            $this->view->query = $queryStr;
        }
    }

    /*
     * Редактирование в админке
     */

    public function modifyAction() {
        
    }

    public function addAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $this->_redirect('/pages/section/id/' . $pid);
    }

    function mb_str_replace($needle, $replacement, $haystack) {
        $needle_len = mb_strlen($needle);
        $replacement_len = mb_strlen($replacement);
        $pos = mb_strpos($haystack, $needle);
        while ($pos !== false) {
            $haystack =
                    mb_substr($haystack, 0, $pos) . $replacement .
                    mb_substr($haystack, $pos + $needle_len);
            $pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
        }
        return $haystack;
    }

    function mb_str_ireplace($co, $naCo, $wCzym) {
        $wCzymM = mb_strtolower($wCzym);
        $coM = mb_strtolower($co);
        $offset = 0;

        while (!is_bool($poz = mb_strpos($wCzymM, $coM, $offset))) {
            $offset = $poz + mb_strlen($naCo);
            $wCzym = mb_substr($wCzym, 0, $poz) . $naCo . mb_substr($wCzym, $poz + mb_strlen($co));
            $wCzymM = mb_strtolower($wCzym);
        }

        return $wCzym;
    }

}